package edu.hunau.cxb19.oop.class27;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttributeView;

import org.junit.Test;

/**
 *
 *@author hb
 *
 * 2020-04-01 16:01:53
 */
public class NioTest {

	@Test
	public void pathTest() {
		Path path1 = Paths.get(".\\demo\\nio.txt");
		System.out.println(path1);
		
		Path path2 = Paths.get(".","demo\\nio.txt");
		System.out.println(path2);

	}
	
	@Test
	public void pathApiTest() {
		Path path1 = Paths.get(".\\demo\\nio.txt");
		Path path2 = Paths.get(".\\demo");
		//判定path1是否以path2开头
		System.out.println(path1.startsWith(path2));
		System.out.println(path1.startsWith("d:"));//false
		//判定path1是否以path2结束
		System.out.println(path1.endsWith("nio.txt"));//true
		
		//判定path是否采用绝对路径
		System.out.println(path1.isAbsolute());//false
		//获得path的父路径
		System.out.println(path1.getParent());
		//获得根路径
		System.out.println(path1.getRoot());
		//获得文件名称
		System.out.println(path1.getFileName());//nio.txt
		
		Path pathN = Paths.get(".\\d00\\d01\\d02\\d03\\nio.txt");
		//获得根路径后面的元素的数量
		System.out.println(path1.getNameCount());//3
		System.out.println(path2.getNameCount());//2
		System.out.println(pathN.getNameCount());//6
		//通过指定索引的位置获得路径的名称
		System.out.println(pathN.getName(2));//d01
		for (int i = 0; i < pathN.getNameCount(); i++) {
			System.out.println(pathN.getName(i));
		}
		//获得Path的绝对路径
		//D:\worksapce\JAVA_WORKSPACE\Eclipse_WORKSPACE\cxb19-java-classcode\java-oop\.\demo\nio.txt
		System.out.println(path1.toAbsolutePath());
		
		//两个路径的合并
		Path p1 = Paths.get("d:\\","nio");
		Path p2 = Paths.get("demo\\nio.txt");
		System.out.println(p1.resolve(p2));// d:\nio\demo\nio.txt
		
		//File和Path可以互转
		File file = p1.toFile();//Path->File
		Path path = file.toPath();//File->path
	}
	
	
	@Test
	public void filesTest01() throws Exception{
		Path nioDir=Paths.get(".\\nio");
		if(!Files.exists(nioDir)) {
			//创建一个目录
			Files.createDirectory(nioDir);
		}
		
	}
}
